/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Add_Iesire.java
 *
 * Created on Jan 12, 2011, 7:31:02 PM
 */

package proiectpractica;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Claudiu
 */
public class Add_Iesire extends javax.swing.JFrame {
        Connection con;
    Statement st;
    int val;
    ArrayList<String > materialNume;
    ArrayList<Integer> materialiId;
    /** Creates new form Add_Intrare */
    /** Creates new form Add_Iesire */
    public Add_Iesire() {
        initComponents();
    }
     public Add_Iesire(Connection c) {
        initComponents();
        con=c;
        materialNume = new ArrayList<String>();
        JOptionPane.showMessageDialog(null, "Form pentru Scoaterea Materialelor din stoc \n Nu lasti nici un camp necompletat!");
        materialiId = new ArrayList<Integer>();
        try {
            st = con.createStatement();


            ResultSet rs=st.executeQuery("select m.idmaterial , nume  from stoc s, materiale m where s.idmaterial= m.idMaterial");

            while (rs.next())
            {
                materialiId.add(rs.getInt(1));
                materialNume.add(rs.getString(2));
                materialBox.addItem(rs.getString(2));

            }
            Calendar cal = Calendar.getInstance();


            java.util.Date date= cal.getTime();

            System.out.println(date.getDate());
            System.out.println(date.getMonth());
            System.out.println(date.getYear());
            LunaComboBox.setSelectedIndex(date.getMonth());
            ZiComboBox.setSelectedIndex(date.getDate()-1);
            AnField.setText(""+(1900+date.getYear()));
//             rs= st.executeQuery("select cod_client from scott.client");
//              while (rs.next())
//            {
//               clientBox.addItem(rs.getInt(1));
//
//            }
            // }
        } catch (Exception ex) {
            Logger.getLogger(Add_Furnizor.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        materialBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cantitateField = new javax.swing.JTextField();
        addjButton2 = new javax.swing.JButton();
        ExitjButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        ZiComboBox = new javax.swing.JComboBox();
        LunaComboBox = new javax.swing.JComboBox();
        AnField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Scoate Material din Depzit");

        jLabel2.setText("Material");

        materialBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                materialBoxActionPerformed(evt);
            }
        });

        jLabel5.setText("cantitate");

        addjButton2.setText("add");
        addjButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addjButton2ActionPerformed(evt);
            }
        });

        ExitjButton1.setText("exit");
        ExitjButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitjButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Data");

        ZiComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        LunaComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }));
        LunaComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LunaComboBoxActionPerformed(evt);
            }
        });

        AnField.setText("2011");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(materialBox, 0, 309, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                                .addComponent(cantitateField, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ZiComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(LunaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(AnField, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(addjButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ExitjButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(materialBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cantitateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ZiComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LunaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addjButton2)
                    .addComponent(ExitjButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void materialBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_materialBoxActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_materialBoxActionPerformed

    private String getDate() throws Exception
    {
        String date =" ";
        String zi  =(String) ZiComboBox.getSelectedItem();
        String luna = (String )LunaComboBox.getSelectedItem();
        try{
            int an  = Integer.parseInt(AnField.getText());
            date =" "+ zi + "-"+luna +"-"+an;
        }catch(Exception ex)
        {
            throw  new Exception("Data  introdusa intr-un format incorect ...")        ;
        }
        return date;
    }
        public int countIesiriMat(int id) {

        try {
            //  aModel.setColumnIdentifiers( jTable1.getColumnModel().getColumns());
           ResultSet  rs = st.executeQuery("select cantitate from stoc  where idmaterial  = "+id);
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();
            int colNo = rsmd.getColumnCount();

                rs.next();
                  BigDecimal ret  = (BigDecimal)rs.getObject(1);
                  System.out.println(" ---> " + ret.intValue() );
                  return ret.intValue();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return 0;
    }
    private void addjButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addjButton2ActionPerformed
        // TODO add your handling code here:
        //val++;
        try {
            if(cantitateField.getText().trim().equalsIgnoreCase("") || AnField.getText().trim().equalsIgnoreCase("")  )
            {
                throw  new Exception ( " Date intrare Incorecte !");
            }
            if(countIesiriMat(materialiId.get( materialBox.getSelectedIndex())) <  Integer.parseInt( cantitateField.getText()))
            {
                throw new Exception(" Cantitate prea mare ");
            }
            CallableStatement cs = con.prepareCall("{call addiesire(?,?,?)}");
            cs.setInt(1, materialiId.get( materialBox.getSelectedIndex()));
            cs.setInt(2, Integer.parseInt( cantitateField.getText()));
            cs.setString(3, " "+ ZiComboBox.getSelectedItem() + "-" +LunaComboBox.getSelectedItem() + " "+AnField.getText());
            System.out.println(" "+ ZiComboBox.getSelectedItem() + "-" +LunaComboBox.getSelectedItem() + " "+AnField.getText());
            System.out.println(" mm "+materialiId.get( materialBox.getSelectedIndex())+" " +Integer.parseInt( cantitateField.getText())+" "+"  ");
            int aux = cs.executeUpdate();
            //st.executeUpdate("call addFurnizorproc ( '"+nume_box.getText()+" ' ,'"+address_box.getText()+" ') ");
            //  st.executeUpdate("insert into scott.client (cod_client , nume , cnp , address) values( '" + val + "' , '" + nume_box.getText() + "' , '" + cnp_box.getText() + "' , '" + address_box.getText()+"' )");
            con.commit();
            JOptionPane.showMessageDialog(null, "nr de intrari afectate "+aux);
        } catch (Exception ex) {
            Logger.getLogger(Add_Furnizor.class.getName()).log(Level.SEVERE, null, ex);
                             JOptionPane.showMessageDialog(null, ex.getMessage());
        }
}//GEN-LAST:event_addjButton2ActionPerformed

    private void ExitjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitjButton1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_ExitjButton1ActionPerformed

    private void LunaComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LunaComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_LunaComboBoxActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Add_Iesire().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField AnField;
    private javax.swing.JButton ExitjButton1;
    private javax.swing.JComboBox LunaComboBox;
    private javax.swing.JComboBox ZiComboBox;
    private javax.swing.JButton addjButton2;
    private javax.swing.JTextField cantitateField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox materialBox;
    // End of variables declaration//GEN-END:variables

}
